/**
 * 
 */
package org.ztest.queue;

import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;

/**
 * @author lhl
 *
 * 2016年1月30日 下午2:13:17
 */
public class SynchronizedQueue<E> implements Queue<E>
{
	private Queue<E> queue;
	private Object mutex;
	
	public SynchronizedQueue(Queue<E> queue)
	{
		this.queue = queue;
		mutex = this;
	}

	@Override
	public int size()
	{
		synchronized (mutex) {return queue.size();}
	}

	@Override
	public boolean isEmpty()
	{
		synchronized (mutex) {return queue.isEmpty();}
	}

	@Override
	public boolean contains(Object o)
	{
		synchronized (mutex) {return queue.contains(o);}
	}

	/**
	 * 迭代器得调用者自己手动同步
	 * @return
	 */
	@Override
	public Iterator<E> iterator()
	{
		return queue.iterator();
	}

	@Override
	public Object[] toArray()
	{
		synchronized (mutex) {return queue.toArray();}
	}

	@Override
	public <T> T[] toArray(T[] a)
	{
		synchronized (mutex) {return queue.toArray(a);}
	}

	@Override
	public boolean remove(Object o)
	{
		synchronized (mutex) {return queue.remove(o);}
	}

	@Override
	public boolean containsAll(Collection<?> c)
	{
		synchronized (mutex) {return queue.containsAll(c);}
	}

	@Override
	public boolean addAll(Collection<? extends E> c)
	{
		synchronized (mutex) {return queue.addAll(c);}
	}

	@Override
	public boolean removeAll(Collection<?> c)
	{
		synchronized (mutex) {return queue.removeAll(c);}
	}

	@Override
	public boolean retainAll(Collection<?> c)
	{
		synchronized (mutex) {return queue.retainAll(c);}
	}

	@Override
	public void clear()
	{
		synchronized (mutex) {queue.clear();}
	}

	@Override
	public boolean add(E e)
	{
		synchronized (mutex) {return queue.add(e);}
	}

	@Override
	public boolean offer(E e)
	{
		synchronized (mutex) {return queue.offer(e);}
	}

	@Override
	public E remove()
	{
		synchronized (mutex) {return queue.remove();}
	}

	@Override
	public E poll()
	{
		synchronized (mutex) {return queue.poll();}
	}

	@Override
	public E element()
	{
		synchronized (mutex) {return queue.element();}
	}

	@Override
	public E peek()
	{
		synchronized (mutex) {return queue.peek();}
	}
}
